class Typeshare < Formula
  desc "Synchronize type definitions between Rust and other languages for seamless FFI"
  homepage "https://github.com/1Password/typeshare"
  url "https://github.com/1Password/typeshare/archive/refs/tags/v1.13.3.tar.gz"
  sha256 "3aa054a8ef566263873a4056be248e6a6790b0815259073ee4a9549229618a09"
  license any_of: ["Apache-2.0", "MIT"]
  head "https://github.com/1Password/typeshare.git", branch: "main"

  livecheck do
    url :stable
    regex(/^v?(\d+(?:\.\d+)+)$/i)
  end

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_sequoia: "a815a340d00c9d99b90f83421907b1c07dab78947d6c8d7c5d6a7f3a6c459aca"
    sha256 cellar: :any_skip_relocation, arm64_sonoma:  "fcbddc02d23fcf0d89e4acec4ee07417ad9636ad522dcd8c2bd1a5f8c88a3618"
    sha256 cellar: :any_skip_relocation, arm64_ventura: "8033f7eaa3404fbfeef3759e82b6d683e82a87f1f1bbec306ef8d3d2a6cc8929"
    sha256 cellar: :any_skip_relocation, sonoma:        "afc917dd47c64d42b99fdcbba61b63fb75699a2daaa1f58026fd08631e24a00c"
    sha256 cellar: :any_skip_relocation, ventura:       "048e626aed9871628b7ba678979bbe2408f571d26ab66446c36593054e43d3ed"
    sha256 cellar: :any_skip_relocation, arm64_linux:   "8a319a9bd392fc23241734a3b6f1cc7c40bbc50b39fad724c20e9fe427c805c7"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "1cff89a63dc6bcdc331facca557f0f641dec53235b0dba99f8d65abdaf35b311"
  end

  depends_on "rust" => :build

  def install
    system "cargo", "install", *std_cargo_args(path: "cli")

    generate_completions_from_executable(bin/"typeshare", "completions")
  end

  test do
    assert_match version.to_s, shell_output("#{bin}/typeshare --version")

    system bin/"typeshare", "--generate-config", testpath
    assert_path_exists testpath/"typeshare.toml"

    (testpath/"example.rs").write <<~RUST
      #[typeshare]
      struct MyStruct {
          my_name: String,
          my_age: u32,
      }
    RUST

    system bin/"typeshare", testpath/"example.rs", "--lang", "typescript", "--output-file", testpath/"TestType.ts"
    assert_match "Generated by typeshare", (testpath/"TestType.ts").read
  end
end
